<template>
  <div class="article-container">
    <!-- 导航栏 -->
    <van-nav-bar
      fixed
      left-arrow
      title="文章详情"
      @click-left="$router.back()"
    />
    <!-- /导航栏 -->

    <!-- 加载中 loading -->
    <van-loading v-if="loading" class="article-loading" type="spinner" />
    <!-- /加载中 loading -->

    <!-- 文章详情 -->
    <div class="detail">
      <h3 class="title">{{ article.title }}</h3>
      <div class="author">
        <van-image round width="1rem" height="1rem" fit="fill" :src="article.aut_photo" />
        <div class="text">
          <p class="name">{{ article.aut_name }}</p>
          <p class="time">{{ article.pubdate | relativeTime }}</p>
        </div>
        <van-button
          v-if="!article.is_followed"
          round
          size="small"
          type="info"
          @click="clickFollow"
        >+ 关注</van-button>
        <van-button
          v-else
          round
          size="small"
          type="danger"
          @click="clickFollow"
        >取 关</van-button>
      </div>
      <div class="content">
        <div v-html="article.content" />
      </div>
      <van-divider>END</van-divider>
      <div class="zan">
        <van-button round size="small" hairline type="primary" plain icon="good-job-o">点赞</van-button>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <van-button round size="small" hairline type="danger" plain icon="delete">不喜欢</van-button>
      </div>
      <!-- 文章评论列表 抽离成子组件 -->
      <comments :art-id="$route.params.id" />
    </div>
    <!-- /文章详情 -->
  </div>
</template>

<script>
import { reqGetArticleInfo } from '@/api/article'
import { reqFollowUser, reqUnFollowUser } from '@/api/user'
import comments from './components/comments.vue'
export default {
  name: 'ArticleIndex',
  components: {
    comments
  },
  data() {
    return {
      loading: true, // 控制加载中的 loading 状态
      article: { }
    }
  },
  async created() {
    const { data: { data }} = await reqGetArticleInfo(this.$route.params.id)
    this.article = data
    // 文章信息请求回来后关闭loading
    this.loading = false
  },
  methods: {
    async clickFollow() {
      // 如果article.is_followed值为false 走关注的业务, 反之取关
      if (this.article.is_followed) {
        // 取关
        await reqUnFollowUser(this.article.aut_id)
      } else {
        // 关注
        await reqFollowUser(this.article.aut_id)
      }
      // 直接更新视图即可
      this.article.is_followed = !this.article.is_followed
    }
  }
}
</script>

<style lang='scss' scoped>
.article-container{
  position: absolute;
  left: 0;
  top: 0;
  overflow-y: scroll;
  width: 100%;
  height: 100%;
}
.article-loading {
  padding-top: 100px;
  text-align: center;
}
.error{
  padding-top: 100px;
  text-align: center;
}
.detail {
  padding: 50px 10px;
  .title {
    font-size: 16px;
  }
  .zan{
    text-align: center;
  }
  .author {
    padding: 10px 0;
    display: flex;
    .text {
      flex: 1;
      padding-left: 10px;
      line-height: 1.3;
      .name {
        font-size: 14px;
        margin: 0;
      }
      .time {
        margin: 0;
        font-size: 12px;
        color: #999;
      }
    }
  }
  .content {
    font-size:14px;
    overflow: hidden;
    white-space: pre-wrap;
    word-break: break-all;
    ::v-deep img{
      max-width:100%;
      background: #f9f9f9;
    }
  }
}
</style>
